﻿using System;
using System.Linq;
using System.Data;
using System.Dynamic;
using System.Security.Cryptography;
using System.Text;
using System.Reflection;
using Nancy;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Dapper;
using API_Service.DBHelper;

namespace API_Service.BizLogic.Platform
{
    public class PlatformPubService : BaseLogicService
    {
        PlatformService service = new PlatformService();

        public PlatformPubService() { }
        public ExecutionResult CheckTokenIsExist(UserDentity dentity)
        {
            var result = new ExecutionResult();

            try
            {
                result.Status = SCUDictionary.Exist(dentity.Token);
            }
            catch (Exception ex)
            {
                Logger.WriteError(MethodBase.GetCurrentMethod(), ex);
                result.Status = false;
                result.Message = "Error: " + ex.Message;
            }

            return result;
        }

        public ExecutionResult<UserDentity> GetUserByToken(string token)
        {
            var result = new ExecutionResult<UserDentity>();

            try
            {
                UserDentity userinfo = SCUDictionary.TokenList[token];

                result.Datas = new UserDentity[] { userinfo };

                result.Status = true;
            }
            catch (Exception ex)
            {
                Logger.WriteError(MethodBase.GetCurrentMethod(), ex);
                result.Status = false;
                result.Message = "Error: " + ex.Message;
            }

            return result;
        }

        private ExecutionResult AddTokenToDict(UserDentity dentity)
        {
            var result = new ExecutionResult();

            try
            {
                result = SCUDictionary.AddItem(dentity.Token, dentity);
            }
            catch (Exception ex)
            {
                Logger.WriteError(MethodBase.GetCurrentMethod(), ex);
                result.Status = false;
                result.Message = "Error: " + ex.Message;
            }

            return result;
        }
        public ExecutionResult<UserDentity> GetToken(UserDentity entity)
        {
            var dentity = service.Login(entity);

            if (dentity.Status && dentity.Datas.Length > 0)
            {
                MD5 md5 = new MD5CryptoServiceProvider();

                byte[] strdata = Encoding.Default.GetBytes(entity.UserID);

                byte[] md5data = md5.ComputeHash(strdata);

                for (int i = 0; i < md5data.Length; i++)
                {
                    entity.Token += md5data[i].ToString("x2");
                }

                entity.UserName = dentity.Datas[0].UserName;

                //write token to server.
                ExecutionResult myToken = this.AddTokenToDict(entity);

                if (myToken.Status)
                {
                    dentity.Token = entity.Token;
                    dentity.Status = true;
                    dentity.Message = "OK";
                    dentity.HttpCode = HttpStatusCode.OK;

                    return dentity;
                }
                else
                {
                    dentity.Token = "";
                    dentity.Status = false;
                    dentity.Message = myToken.Message;
                    dentity.HttpCode = HttpStatusCode.Unauthorized;

                    return dentity;
                }
            }
            else
            {
                dentity.Token = "";
                dentity.Status = false;
                dentity.Message = "账户或密码错误, 没有权限访问服务器.";
                dentity.HttpCode = HttpStatusCode.Unauthorized;

                return dentity;
            }
        }
    }
}